import moment from 'moment'

// 保留两位小数
let toDecimal2 = x => {
  let f1 = parseFloat(x)
  if (isNaN(f1)) {
    return false
  }
  let f = Math.round(x * 100) / 100
  let s = f.toString()
  let rs = s.indexOf('.')
  if (rs < 0) {
    rs = s.length
    s += '.'
  }
  while (s.length <= rs + 2) {
    s += '0'
  }
  return s
}
// 百分比
let percentage = x => {
  let str = Number(x * 100).toFixed(2)
  str += '%'
  return str
}
// 价格
let currency = s => {
  if (typeof s !== 'number') {
    return '--'
  }
  var n = 2
  s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''// eslint-disable-line
  var l = s.split('.')[0].split('').reverse()
  var r = s.split('.')[1]
  var t = ''
  for (let i = 0; i < l.length; i++) {
    t += l[i] + ((i + 1) % 3 === 0 && i + 1 !== l.length ? ',' : '')
  }
  return (
    t.split('').reverse().join('') + '.' + r
  )
}
// 生日过滤
let jsGetAge = Birthday => {
  if (Birthday) {
    var returnAge
    let strBirthday = Birthday.substring(0, 10)
    var strBirthdayArr = strBirthday.split('-')
    var birthYear = Number(strBirthdayArr[0])
    var birthMonth = Number(strBirthdayArr[1])
    var birthDay = Number(strBirthday.split('-')[2])

    let d = new Date()
    var nowYear = d.getFullYear()
    var nowMonth = d.getMonth() + 1
    var nowDay = d.getDate()
    if (nowYear === birthYear) {
      returnAge = 0 // 同年 则为0岁
    } else {
      var ageDiff = nowYear - birthYear // 年之差

      if (ageDiff > 0) {
        if (nowMonth === birthMonth) {
          var dayDiff = nowDay - birthDay // 日之差

          if (dayDiff < 0) {
            returnAge = ageDiff - 1
          } else {
            returnAge = ageDiff
          }
        } else {
          var monthDiff = nowMonth - birthMonth // 月之差
          if (monthDiff < 0) {
            returnAge = ageDiff - 1
          } else {
            returnAge = ageDiff
          }
        }
      } else {
        returnAge = 0 // 返回-1 表示出生日期输入错误 晚于今天
      }
    }
    return returnAge // 返回周岁年龄
  }
}
// 性别
let sex = s => {
  switch (s) {
    case 'other':
      return '未知'
    case 'mister':
      return '男'
    case 'lady':
      return '女'
  }
}

// 账户状态（联系人列表、用户列表状态）
let statusUser = userStatu => {
  if (userStatu === true || userStatu === 1 || userStatu === 'true') {
    return '正常'
  }
  if (userStatu === false || userStatu === 0 || userStatu === 'false') {
    return '已冻结'
  }
}

// 执行一次
let oneTime = one => {
  switch (one) {
    case true:
      return '是'
    case false:
      return '否'
  }
}


export {
  toDecimal2,
  sex,
  oneTime,
  statusUser,
  currency,
  jsGetAge,
  percentage
}
